#include "test/example/echo_server/echoServer.h"
#include "net/tcp_server.h"
#include "net/buffer.h"

using namespace net;

void EchoServerProcessor::HandleRead(TcpStream* stream, Buffer* buffer) {
  size_t len = buffer->ReadableSize();
  u_char* buf = buffer->Retrieve(len);
  LOG(INFO)<< "from fd: " << stream->Fd() << "  recv: " << buf << std::endl;

  stream->Send(buf, len, NULL);
}

void EchoServerProcessor::HandleWrite(TcpStream* stream, Buffer* buffer) {
  LOG(INFO)<< "should not be called...";
}

EchoServer::EchoServer(net::Poller* poller, const std::string& ip, uint16 port)
    : tcp_server_(new net::TcpServer(poller, ip, port)) {
}

void EchoServer::Start() {
  tcp_server_->SetProcessor(new EchoServerProcessor);
  tcp_server_->Start();
}

int main(int argc, char*argv[]) {
  Poller* poller = CreatePoller();
  EchoServer echo(poller, "", 8888);
  echo.Start();

  return 0;
}
